<template>
	<view class="pages">
		<view class="img-container">
			<view class="greeting">
				您好，
				<br />
				欢迎使用进销存系统
			</view>
		</view>
		<view class="login-view">
			<view class="login-form">
				<form class="form">
					<view class="form-group">
						<text class="label">手机号</text>
						<input type="number" name="accountName" placeholder="请输入您的手机号" maxlength="11" v-model="forms.accountName" />
					</view>
					<view class="form-group">
						<text class="label">密码</text>
						<input type="password" name="loginPass" maxlength="18" placeholder="请输入您的密码" v-model="forms.loginPass" />
					</view>
					<button @tap="login()">登录</button>
					<!-- <view class="register" @tap="register()">注册</view> -->
				</form>
			</view>
		</view>
		<!-- ends -->
		<agree-vue @change="checkBoxChanage" />
	</view>
</template>

<script setup>
import { onLoad, onShow } from '@dcloudio/uni-app';
import { onBeforeMount, onMounted, reactive, ref, toRefs, getCurrentInstance } from 'vue';
import { storeToRefs } from 'pinia';
import http from '@/common/request.js';
import { urlToObj, sgoto, navTo, getImgSrc, setLoadingToast, setToast, setUserIdentity } from '@/common/utils.js';
import useStore from '@/store';
import agreeVue from '@/components/agree.vue';
import { getCode } from '../../../common/wx';

// 宇硕全局状态管理器
const { useLogisicsYushuo, useWx } = useStore();

// state
const { userInfo } = storeToRefs(useLogisicsYushuo);

// action
const { companyLogin, getToken } = useLogisicsYushuo;

const { getOpenId } = storeToRefs(useWx);

// 登录表单
const forms = reactive({
	accountName: '',
	loginPass: '',
	captcha: '',
	isLock: false
});

const checked = ref(false);

const checkBoxChanage = (e) => {
	console.log('e ====> ', e);
	checked.value = e;
};
// 状态控制
const lockClick = (lock) => {
	forms.isLock = lock;
};

// 登录
const login = () => {
	// const regxPone = /^1[3-9]\d{9}$/;
	// if (!regxPone.test(forms.accountName)) {
	// 	setToast('请填写正确的账号');
	// 	return;
	// }
	if (!forms.accountName) {
		setToast('请填写密码');
		return;
	}
	if (!checked.value) {
		setToast('请同意并勾选下方政策协议后操作~');
		return;
	}
	// 登陆
	try {
		// 绑定openid
		// console.log('getOpenId =============> ', getOpenId);
		forms.openid = getOpenId;
		companyLogin(forms);
	} catch (e) {
		//TODO handle the exception
		uni.hideLoading();
	}
};

//
onMounted(() => {
	// 判断是否已经登陆了 如果有token
	setUserIdentity();
	// 获取code
	getCode();
	// console.log('getToken ==========> ', getToken);
	if (getToken) {
		navTo('/pages/yuShuo/index/index');
	}
});
</script>

<style scoped lang="scss">
@import './login.scss';
</style>
